package com.poetize.plus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poetize.plus.entity.dto.FriendDTO;
import com.poetize.plus.entity.dto.page.FriendPageDTO;
import com.poetize.plus.entity.po.Friend;
import com.poetize.plus.entity.vo.FriendVO;
import com.poetize.plus.entity.vo.page.FriendPageVO;
import com.poetize.plus.mapper.FriendMapper;
import com.poetize.plus.service.FriendService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zuosy
 * @created 2024/5/14 17:06
 */
@Service
@RequiredArgsConstructor
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {

    @Override
    public Page<FriendPageVO> friendPage(FriendPageDTO friendPageDTO) {
        Page<Friend> friendPage = lambdaQuery()
                .like(StringUtils.hasText(friendPageDTO.getName()), Friend::getName, friendPageDTO.getName())
                .eq(StringUtils.hasText(friendPageDTO.getType()), Friend::getType, friendPageDTO.getType())
                .page(Page.of(friendPageDTO.getCurrent(), friendPageDTO.getSize()));
        List<Friend> records = friendPage.getRecords();
        if (CollectionUtils.isEmpty(records)){
            return new Page<>();
        }

        Page<FriendPageVO> result = new Page<>();
        result.setRecords(FriendPageVO.convertPageList(records));
        result.setTotal(friendPage.getTotal());
        return result;
    }

    @Override
    public FriendVO queryById(Long id) {
        Friend friend = baseMapper.selectById(id);
        return Optional.ofNullable(friend)
                .map(FriendVO::convert)
                .orElseThrow(() -> new IllegalArgumentException("数据异常"));
    }

    @Override
    public Boolean updateById(FriendDTO friendDTO) {
        Friend friend = FriendDTO.convertDTO(friendDTO);
        return baseMapper.updateById(friend) > 0;
    }

    @Override
    public Map<String, List<FriendVO>> getFriends() {
        List<Friend> friendList = lambdaQuery()
                .eq(Friend::getStatus, true)
                .list();
        List<FriendVO> convertedList = FriendVO.convertList(friendList);
        return convertedList.stream().collect(Collectors.groupingBy(FriendVO::type));
    }


    @Override
    public Boolean add(FriendDTO friendDTO) {
        Friend checkName = lambdaQuery().eq(Friend::getName, friendDTO.name()).one();
        if (Objects.nonNull(checkName)){
            if (checkName.getStatus()){
                throw new IllegalArgumentException("该网站名称已存在");
            }else {
                throw new IllegalArgumentException("该网站名称待审核中");
            }
        }

        Friend checkUrl = lambdaQuery().eq(Friend::getUrl, friendDTO.url()).one();
        if (Objects.nonNull(checkUrl)){
            if (checkUrl.getStatus()){
                throw new IllegalArgumentException("该域名已存在");
            }else {
                throw new IllegalArgumentException("该域名待审核中");
            }
        }

        Friend friend = FriendDTO.convertDTO(friendDTO);
        return baseMapper.insert(friend) > 0;
    }

    @Override
    public Boolean changeStatus(Long id, Boolean status) {
        return lambdaUpdate().set(Friend::getStatus, status).eq(Friend::getId, id).update();
    }
}
